package com.ruoyi.equ.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.equ.domain.EquLabels;
import com.ruoyi.equ.service.IEquLabelsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备标签信息Controller
 * 
 * @author lgq
 * @date 2023-10-07
 */
@RestController
@RequestMapping("/equ/labels")
public class EquLabelsController extends BaseController
{
    @Autowired
    private IEquLabelsService equLabelsService;

    /**
     * 查询设备标签信息列表
     */
    @PreAuthorize("@ss.hasPermi('equ:labels:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquLabels equLabels)
    {
        startPage();
        List<EquLabels> list = equLabelsService.selectEquLabelsList(equLabels);
        return getDataTable(list);
    }

    /**
     * 导出设备标签信息列表
     */
    @PreAuthorize("@ss.hasPermi('equ:labels:export')")
    @Log(title = "设备标签信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquLabels equLabels)
    {
        List<EquLabels> list = equLabelsService.selectEquLabelsList(equLabels);
        ExcelUtil<EquLabels> util = new ExcelUtil<EquLabels>(EquLabels.class);
        util.exportExcel(response, list, "设备标签信息数据");
    }

    /**
     * 获取设备类别细类详细信息
     */
    @GetMapping("/getInfoByEquId/{equId}")
    public AjaxResult getInfoByEquId(@PathVariable("equId") String equId)
    {
        return AjaxResult.success(equLabelsService.selectEquLabelsByEquId(equId));
    }

    /**
     * 获取设备标签信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('equ:labels:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(equLabelsService.selectEquLabelsById(id));
    }

    /**
     * 新增设备标签信息
     */
    @PreAuthorize("@ss.hasPermi('equ:labels:add')")
    @Log(title = "设备标签信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquLabels equLabels)
    {
        //删除原本标签信息
        int res = equLabelsService.deleteEquLabelsByEquId(equLabels.getEquId());
        //新增标签信息
        equLabels.setId(IdUtils.fastSimpleUUID());
        return toAjax(equLabelsService.insertEquLabels(equLabels));
    }

    /**
     * 修改设备标签信息
     */
    @PreAuthorize("@ss.hasPermi('equ:labels:edit')")
    @Log(title = "设备标签信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquLabels equLabels)
    {
        int ret;
        //判断标签内容是否为空，为空则删除标签信息
        if(StringUtils.isEmpty(equLabels.getTypeLabel())) {
            ret = equLabelsService.deleteEquLabelsById(equLabels.getId());
        }else {
            ret = equLabelsService.updateEquLabels(equLabels);
        }
        return toAjax(ret);
    }

    /**
     * 删除设备标签信息
     */
    @PreAuthorize("@ss.hasPermi('equ:labels:remove')")
    @Log(title = "设备标签信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(equLabelsService.deleteEquLabelsByIds(ids));
    }
}
